package com.hs.panghu.servlet;

import com.alibaba.fastjson.JSON;
import com.hs.panghu.dao.ShopDao;
import com.hs.panghu.dao.impl.ShopDaoImpl;
import com.hs.panghu.entity.Goods;
import com.hs.panghu.entity.Shop;
import com.hs.panghu.util.Result;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/shop")
public class ShopServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String action = req.getParameter("action");
        // 连接数据库
        ShopDao shopDao = new ShopDaoImpl();
        if ("shopList".equals(action)) {  // 分页查询
            Map<String, String> map = new HashMap<>();
            String pageNum = req.getParameter("pageNum");
            String pageSize = req.getParameter("pageSize");
            String phone = req.getParameter("phone");
            String shopStatus = req.getParameter("shopStatus");
            String keyword = req.getParameter("keyword");
            String startTime = req.getParameter("startTime");
            String endTime = req.getParameter("endTime");
            map.put("pageNum", pageNum);
            map.put("pageSize", pageSize);
            map.put("phone", phone);
            map.put("keyword", keyword);
            map.put("shopStatus", shopStatus);
            map.put("startTime", startTime);
            map.put("endTime", endTime);

            // 动态获取社区集合信息
            List<Shop> list = shopDao.selectShops(map);
            // 动态获取社区的数据总条数
            int total = shopDao.selectShopCount(map);
            // 创建一个返回值的map集合，用来包裹list数据和total总数量
            Map resultMap = new HashMap();
            resultMap.put("list", list);
            resultMap.put("total", total);
            //把结果集合，封装到result对象中
            Result success = Result.success(resultMap);
            resp.getWriter().write(JSON.toJSONString(success));
        } else if ("shopAdd".equals(action)) { //添加
            String shopAddress = req.getParameter("shopAddress");
            String shopCity = req.getParameter("shopCity");
            String shopDescribe = req.getParameter("shopDescribe");
            String shopDistrict = req.getParameter("shopDistrict");
            String shopImage = req.getParameter("shopImage");
            String shopLatitude = req.getParameter("shopLatitude");
            String shopLongitude = req.getParameter("shopLongitude");
            String shopName = req.getParameter("shopName");
            String shopPhone = req.getParameter("shopPhone");
            String shopProvince = req.getParameter("shopProvince");
            //组成对象
            Shop shop = new Shop(
                    shopAddress,
                    shopCity,
                    shopDescribe,
                    shopDistrict,
                    shopImage,
                    shopLatitude,
                    shopLongitude,
                    shopName,
                    shopPhone,
                    shopProvince);
            int row = shopDao.insertShop(shop);
            if (row != 0) {
                Result success = Result.success();
                resp.getWriter().write(JSON.toJSONString(success));
            } else {
                Result error = Result.error();
                resp.getWriter().write(JSON.toJSONString(error));
            }
        }else if("shopUpdate".equals(action)){
//            String addTime = req.getParameter("addTime");
            String concernStatus = req.getParameter("concernStatus");
            String distance = req.getParameter("distance");
            String salesVolume = req.getParameter("salesVolume");
            String shopAccount = req.getParameter("shopAccount");
            String shopAddress = req.getParameter("shopAddress");
            String shopCategoryList = req.getParameter("shopCategoryList");
            String shopCity = req.getParameter("shopCity");
            String shopCommunitySort = req.getParameter("shopCommunitySort");
            String shopDescribe = req.getParameter("shopDescribe");
            if(shopDescribe==null || "".equals(shopDescribe)){
                shopDescribe = "0";
            }
            String shopDistrict = req.getParameter("shopDistrict");
            String shopGoodsList = req.getParameter("shopGoodsList");
            String shopId = req.getParameter("shopId");
            String shopImage = req.getParameter("shopImage");
            if(shopImage==null || "".equals(shopImage)){
                shopImage = "0";
            }
            String shopLatitude = req.getParameter("shopLatitude");
            String shopLongitude = req.getParameter("shopLongitude");
            String shopName = req.getParameter("shopName");
            String shopOrderCount = req.getParameter("shopOrderCount");
            String shopPassword = req.getParameter("shopPassword");
            String shopPhone = req.getParameter("shopPhone");
            String shopProvince = req.getParameter("shopProvince");
            String shopSort = req.getParameter("shopSort");
            String shopStatus = req.getParameter("shopStatus");
            String shopUserId = req.getParameter("shopUserId");
            //组成对象
            Shop shop = new Shop(
            Integer.parseInt(shopId),
            Integer.parseInt(concernStatus),
            distance,
            salesVolume,
            shopAccount,
            shopAddress,
            shopCategoryList,
            shopCity,
            shopCommunitySort,
            shopDescribe,
            shopDistrict,
            shopGoodsList,
            shopImage,
            shopLatitude,
            shopLongitude,
            shopName,
            Integer.parseInt(shopOrderCount),
            shopPassword,
            shopPhone,
            shopProvince,
            Integer.parseInt(shopSort),
            Integer.parseInt(shopStatus),
            Integer.parseInt(shopUserId)
            );
            int row = shopDao.updateShop(shop);
            if (row != 0) {
                Result success = Result.success();
                resp.getWriter().write(JSON.toJSONString(success));
            } else {
                Result error = Result.error();
                resp.getWriter().write(JSON.toJSONString(error));
            }
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
